package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ChangeLanguageActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private FrameLayout mFlEnglish, mFlDeutsch, mFlFrench, mFlSvenska;
    private ImageView mIvEnglish, mIvDeutsh, mIvFrench, mIvSvenska;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;
    private Configuration mConfiguration;
    private DisplayMetrics mDisplayMetrics;
    private String languageMode;
    List<View> list;
    static public final Locale SWEDISH = new Locale("sv", "Swedish");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_language);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = ChangeLanguageActivity.this;
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);
        mEditor = mPreferences.edit();
        mConfiguration = getResources().getConfiguration();
        mDisplayMetrics = getResources().getDisplayMetrics();
        initView();
        initData();
    }

    public void initView() {
        mLinearLayout = findViewById(R.id.ll_change_language);
        mLinearLayout.setOnClickListener(this);

        mFlEnglish = findViewById(R.id.fl_change_language_english);
        mFlEnglish.setOnClickListener(this);

        mFlDeutsch = findViewById(R.id.fl_change_language_deutsch);
        mFlDeutsch.setOnClickListener(this);

        mFlFrench = findViewById(R.id.fl_change_language_french);
        mFlFrench.setOnClickListener(this);

        mFlSvenska = findViewById(R.id.fl_change_language_svenska);
        mFlSvenska.setOnClickListener(this);

        mIvEnglish = findViewById(R.id.iv_change_language_english);

        mIvDeutsh = findViewById(R.id.iv_change_language_deutsch);

        mIvFrench = findViewById(R.id.iv_change_language_french);

        mIvSvenska = findViewById(R.id.iv_change_language_svenska);

        list = new ArrayList<>();
        list.add(mIvEnglish);
        list.add(mIvDeutsh);
        list.add(mIvFrench);
        list.add(mIvSvenska);
    }

    public void initData() {
        languageMode = mPreferences.getString("language", Locale.getDefault().getLanguage());
        switch (languageMode) {
            case "en":  //英语
                setView(list, 0);
                break;
            case "fr":  //法语
                setView(list, 2);
                break;
            case "de":  //德语
                setView(list, 1);
                break;
            case "sv":  //瑞典语
                setView(list, 3);
                break;
        }
    }

    public void setView(List<View> list, int position) {
        for (int i = 0; i < list.size(); i++) {
            if (i == position) {
                list.get(i).setVisibility(View.VISIBLE);
            } else {
                list.get(i).setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_change_language:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.fl_change_language_english:
                if (languageMode.equals("en")) {

                } else {
                    setView(list, 0);
                    mConfiguration.setLocale(Locale.ENGLISH);
                    getResources().updateConfiguration(mConfiguration, mDisplayMetrics);
                    mEditor.putString("language", "en");
                    mEditor.commit();
                    restartApplication();
                }
                break;
            case R.id.fl_change_language_deutsch:
                //ToastUtils.showToast(mContext, getResources().getString(R.string.lan_no_support));
                if (languageMode.equals("de")) {

                } else {
                    setView(list, 1);
                    mConfiguration.setLocale(Locale.GERMAN);
                    getResources().updateConfiguration(mConfiguration,mDisplayMetrics);
                    mEditor.putString("language", "de");
                    mEditor.commit();
                    restartApplication();
                }
                break;
            case R.id.fl_change_language_french:
                ToastUtils.showToast(mContext, getResources().getString(R.string.lan_no_support));
//                if (languageMode.equals("fr")) {
//
//                } else {
//                                    setView(list, 2);
//                    mConfiguration.setLocale(Locale.FRENCH);
//                    getResources().updateConfiguration(mConfiguration,mDisplayMetrics);
//                    mEditor.putString("language", "fr");
//                    mEditor.commit();
//                    restartApplication();
//                }
                break;
            case R.id.fl_change_language_svenska:
                ToastUtils.showToast(mContext, getResources().getString(R.string.lan_no_support));
//                mConfiguration.setLocale(SWEDISH);
//                if (languageMode.equals("sv")) {
//
//                } else {
//                   setView(list, 3);
//                    mConfiguration.setLocale(SWEDISH);
//                    getResources().updateConfiguration(mConfiguration,mDisplayMetrics);
//                    mEditor.putString("language", "sv");
//                    mEditor.commit();
//                    restartApplication();
//                }
                break;
        }
    }

    private void restartApplication() {
        final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}
